package com.ruicar.afs.cloud.batch.frame.control;

import cn.hutool.core.thread.ThreadUtil;
import com.ruicar.afs.cloud.batch.enums.MessageType;
import com.ruicar.afs.cloud.batch.frame.logger.WsMonitorHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MonitorControl extends Thread{
    private List<BatchControlRunner> batchControlRunners = new ArrayList<>();
    private boolean finished = false;
    public void setFinished(){
        this.finished = true;
    }
    public void add(BatchControlRunner batchControlRunner){
        this.batchControlRunners.add(batchControlRunner);
    }

    @Override
    public void run(){
        while (!finished){
            batchControlRunners
                    .stream()
                    .filter(batchControlRunner -> !batchControlRunner.isSelfFinished())
                    .forEach(BatchControlRunner::logRunInfo);
            ThreadUtil.sleep(500, TimeUnit.MILLISECONDS);
        }
        WsMonitorHelper.sendMessage(MessageType.INFO,"======批量调度完成=======");
        batchControlRunners.clear();
    }
}
